<?php

$session = True;

session_set_save_handler(
	'pxf_session_open',
	'pxf_session_close',
	'pxf_session_read',
	'pxf_session_write',
	'pxf_session_destroy',
	'pxf_session_clean'
);

function pxf_session_open(){}
function pxf_session_close(){}
function pxf_session_clean($max){} 

function pxf_session_read($id)
{
	$sessions = PXFModel::getModel('session'); 
	$session = $sessions->findBy(array('condition' => new PXFCondition('hash',$id)));
	if(count($session) ===  0)
	{
		return '';
	}
	else
	{
		$session = $session[0];
		return $session->data;
	}
}

function pxf_session_write($id, $data)
{   
	$mod_sessions = PXFModel::getModel('session'); 
	$session = $mod_sessions->findBy(
			array('condition' => new PXFCondition('hash',$id)));

	if(count($session) ===  0)
	{
		$session = $mod_sessions->getNew();
		$session->created = date('Y-m-d h:i:s');
		$session->updated = date('Y-m-d h:i:s');
	}
	else
	{
		$session = $session[0];
		$session->updated = date('Y-m-d h:i:s');
	}

	$session->hash = $id;
	$session->data = mysql_real_escape_string($data);
	$session->active = 1;
	$session->destroyed = 1;
	$session->save();
}

function pxf_session_destroy($id)
{
	$mod_sessions = PXFModel::getModel('session'); 
	$session = $mod_sessions->findBy(
			array('condition' => new PXFCondition('hash',$id)));

	if(count($session) > 0)
	{
		$session->delete();
	}
}
?>
